package com.adtech.core.web.action;

import java.io.Serializable;
import java.util.UUID;

/**
 * Ajax异步请求返回结果
 * 
 * @since 2011-11-4
 * @author Dormin
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public final class AjaxResult implements Serializable {

	private static final long serialVersionUID = -7732305001679226712L;

	public static final byte STATUS_SUCCESS = 0;
	public static final byte STATUS_FAILURE = 1;

	private byte status = STATUS_SUCCESS;
	private int code;
	private String message;
	private Object data;


	private AjaxResult(){
		
	
	}

	/**
	 * 构造一个状态为“成功”的响应（不带数据和消息）
	 * @return
	 *
	 * @author Dormin
	 */
	public static AjaxResult success(){
		AjaxResult result = new AjaxResult();
		result.status = STATUS_SUCCESS;

		return result;
	}

	/**
	 * 构造一个状态为“成功”的响应（带数据不带消息）
	 * @return
	 *
	 * @author Dormin
	 */
	public static AjaxResult success(Object data){
		AjaxResult result = new AjaxResult();
		result.setStatus(STATUS_SUCCESS);
		result.setData(data);

		return result;
	}

	/**
	 * 构造一个状态为“成功”的响应（带数据和消息）
	 * @return
	 *
	 * @author Dormin
	 */
	public static AjaxResult success(Object data, String message){
		AjaxResult result = new AjaxResult();
		result.setStatus(STATUS_SUCCESS);
		result.setData(data);
		result.setMessage(message);

		return result;
	}

	/**
	 * 构造一个状态为“失败”的响应（带业务异常错误码）
	 * @return
	 *
	 * @author Dormin
	 */
	public static AjaxResult failed(int code){
		AjaxResult result = new AjaxResult();
		result.setStatus(STATUS_FAILURE);
		result.setCode(code);

		return result;
	}

	/**
	 * 构造一个状态为“失败”的响应（带业务异常错误码和错误消息）
	 * @return
	 *
	 * @author Dormin
	 */
	public static AjaxResult failed(int code, String message){
		AjaxResult result = new AjaxResult();
		result.setStatus(STATUS_FAILURE);
		result.setCode(code);
		result.setMessage(message);

		return result;
	}

	public byte getStatus() {
		return status;
	}

	public void setStatus(byte status) {
		this.status = status;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
	public String getUuid() {
		return UUID.randomUUID().toString();
	}


}